[C] Agenda - LDE
Publicado por Enzo de Brito Ferber (última atualização em 15/09/2010)
[ Hits: 6.577 ]
Homepage: http://www.maximasonorizacao.com.br
Agenda simples com funções para inserir, remover e procurar. Uso de listas duplamente encadeadas.
http://www.vivaolinux.com.br/artigo/Linguagem-C-Listas-Duplamente-Encadeadas/
// agenda.c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #define MALLOC(a) (a *) malloc( sizeof( a )) #define MAX 50 // descomentar abaixo em linux #define fflush(stdin) __fpurge(stdin) struct contato { char nome[ MAX ]; char telefone[ MAX ]; char celular[ MAX ]; char email[ MAX ]; char obs[ MAX ]; struct contato *next; struct contato *prior; }; // variaveis para começo e fim da lista struct contato *head; struct contato *last; void inserirdados( struct contato *novo ) { struct contato *current; // primeiro elemento da lista if( !head ) { head = last = novo; novo->next = NULL; novo->prior = NULL; return ; } current = head; while( current ) { // comparação.... if( strcmp( current->nome, novo->nome ) < 0 ) current = current->next; else { // elemento no meio da lista if( current->prior ) { novo->next = current; novo->prior = current->prior; current->prior->next = novo; current->prior = novo; return ; } // novo primeiro elemento da lista novo->next = current; novo->prior = NULL; head->prior = novo; head = novo; return ; } } // ultimo elemento da lsta novo->next = NULL; novo->prior = last; last->next = novo; last = novo; return ; } struct contato *pesquisar( char *nome ) { struct contato *current; current = head; while( current ) { if( strcmp( current->nome, nome ) == 0 ) return current; current = current->next; } return NULL; } void removercontato( struct contato *del ) { // unico item if ( head == last ) { free( head ); head = last = NULL; return ; } // primeiro item if( !del->prior ) { head = del->next; head->prior = NULL; free( del ); return ; } // ultimo item if( !del->next ) { last = del->prior; last->next = NULL; free( del ); return ; } // item no meio del->prior->next = del->next; del->next->prior = del->prior; free( del ); return ; } void interfaceinserir( void ) { struct contato *novo = MALLOC( struct contato ); // cls - windows // clear - linux system( "clear" ); printf( "\tNome : " ); fflush( stdin ); scanf( "%[^\n]", novo->nome ); printf( "\tTelefone : " ); fflush( stdin ); scanf( "%[^\n]", novo->telefone ); printf( "\tCelular : " ); fflush( stdin ); scanf( "%[^\n]", novo->celular ); printf( "\tE-mail : " ); fflush( stdin ); scanf( "%[^\n]", novo->email ); printf( "\tObservacoes: " ); fflush( stdin ); scanf( "%[^\n]", novo->obs ); // inserir inserirdados( novo ); return ; } void interfaceremover( void ) { char remover[ MAX ]; struct contato *del; // cls - windows // clear - linux system( "clear" ); printf( "\tNome : " ); fflush( stdin ); scanf( "%[^\n]", remover ); del = pesquisar( remover ); if( del ) removercontato( del ); } void interfacepesquisar( void ) { struct contato *show; char nome[ MAX ]; // cls - windows // clear - linux system( "clear" ); printf( "\tNome : " ); fflush( stdin ); scanf( "%[^\n]", nome ); show = pesquisar( nome ); if ( !show ) return ; // cls - windows // clear - linux system( "clear" ); puts( "\n" ); printf( "\tNome : %s\n", show->nome ); printf( "\tTelefone : %s\n", show->telefone ); printf( "\tCelular : %s\n", show->celular ); printf( "\tE-mail : %s\n", show->email ); printf( "\tObservacoes: %s\n", show->obs ); fflush( stdin ); getchar(); return ; } void destruirlista( void ) { struct contato *current; current = head; if( !head ) return ; while( current->next ) { current = current->next; free( current->prior ); } free( last ); return ; } void sair( void ) { destruirlista(); exit( EXIT_SUCCESS ); } void listar( void ) { struct contato *current; // cls - windows // clear - linux system( "clear" ); current = head; if( !head ) { printf( "Lista vazia.\n"); fflush( stdin ); getchar(); } while( current ) { printf( "%s\n", current->nome ); current = current->next; } fflush( stdin ); getchar(); return ; } // matriz de ponteiros para função // o uso deste recurso faz com que o uso de switch...case seja dispensável void (* funcs[5])(void) = {interfaceinserir, interfaceremover, interfacepesquisar, listar, sair}; void interfaceprincipal( void ) { int op; while( 1 ) { // cls - windows // clear - linux system( "clear" ); printf( "\tMENU\n\n" ); printf( "\t1. INSERIR\n" ); printf( "\t2. REMOVER\n" ); printf( "\t3. INFORMACOES\n" ); printf( "\t4. LISTAR\n" ); printf( "\t5. SAIR\n" ); printf( "\n\t$ " ); fflush( stdin ); scanf( "%d", &op ); if( op >= 1 && op <= 5 ) (*funcs[op - 1])(); } return ; } int main ( void ) { interfaceprincipal(); return ; }
Preloader.c - Adaptação do Tarik Ahmad (Thiago Alexandre) para linux
Calculadora elementar com ponto flutuante
Lista simplesmente encadeada C
Nenhum coment�rio foi encontrado.
Conciliando o uso da ZRAM e SWAP em disco na sua máquina
Servidor de Backup com Ubuntu Server 24.04 LTS, RAID e Duplicati (Dell PowerEdge T420)
Visualizar câmeras IP ONVIF no Linux sem necessidade de instalar aplicativos
Realizar overclock no Miyoo Mini (plus ou normal)
Otimização de memória para máquinas modestas
Unbuntu não atualiza o firmware [RESOLVIDO] (2)
linux mint reconhece microfone de lapela como fone de ouvido sem micro... (0)
Dúvidas sobre a originalidade de conteúdos online (10)
Erro de interface de Rede no Virt Manager dentro Debian 13 KDE (12)